package com.industrial.loweff.controller.lowcompany;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.loweff.domain.CityLevelCdCount;
import com.industrial.loweff.domain.JcQueryParam;
import com.industrial.loweff.service.ILowEfficiencyEnterprisesAnalysisService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/low_eff_company_analysis")
public class LowEfficiencyEnterprisesAnalysisController {

    @Autowired
    private ILowEfficiencyEnterprisesAnalysisService lowEfficiencyEnterprisesAnalysisService;


    /**
     * 低效能指标情况分布
     * @return
     */
    @GetMapping("/queryLoweIndexFbInfo")
    public AjaxResult queryLoweIndexFbInfo(JcQueryParam params){

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else{
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }

        Map<String,Object> result = lowEfficiencyEnterprisesAnalysisService.queryLoweIndexFbInfo(params);
        return AjaxResult.success(result);
    }

    /**
     * <p>
     *     查询地图数据
     * </p>
     * @return
     */
    @GetMapping("/queryMapData")
    public AjaxResult queryMapData(JcQueryParam params){

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else{
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }

        List<CityLevelCdCount> cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisService.queryMapData(params);
        return AjaxResult.success(cityLevelCdCounts);
    }

    /**
     * 查询近五年的数据
     * @param params
     * @return
     */
    @GetMapping("/queryFiveData")
    public AjaxResult queryFiveData(JcQueryParam params){

        if(StringUtils.isBlank(params.getYears())){
            return AjaxResult.warn("请传入年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else{
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }


        Integer currentYear = Integer.valueOf(params.getYears());
        Integer preFiveYear = currentYear - 5;
        params.setEndYear(String.valueOf(preFiveYear));

        Map<String, Object> resultMap = lowEfficiencyEnterprisesAnalysisService.queryFiveData(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询低效能企业行业分布
     * @return
     */
    @GetMapping("/queryLoweIndustry")
    public AjaxResult queryLoweIndustry(JcQueryParam params){
        if(StringUtils.isBlank(params.getYears())){
            return AjaxResult.warn("请传入年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else{
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }

        Map<String, Object> resultMap = lowEfficiencyEnterprisesAnalysisService.queryLoweIndustry(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 低效能企业地市分布
     * @param params
     * @return
     */
    @GetMapping("/queryLoweCity")
    public AjaxResult queryLoweCity(JcQueryParam params){
        if(StringUtils.isBlank(params.getYears())){
            return AjaxResult.warn("请传入年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else {
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }



        Map<String,Object> resultMap = lowEfficiencyEnterprisesAnalysisService.queryLoweCity(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 低效能企业研发强度
     * @param params
     * @return
     */
    @GetMapping("/queryLowCompanyYfQd")
    public AjaxResult queryLowCompanyYfQd(JcQueryParam params){
        if(StringUtils.isBlank(params.getYears())){
            return AjaxResult.warn("请传入年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else {
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }



        Map<String,Object> resultMap = lowEfficiencyEnterprisesAnalysisService.queryLowCompanyYfQd(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * <p>
     *     低效能占用土地面积占比情况
     * </p>
     * @return
     */
    @GetMapping("/queryLowYdmmzb")
    public AjaxResult queryLowYdmmzb(JcQueryParam params){
        if(StringUtils.isBlank(params.getYears())){
            return AjaxResult.warn("请传入年份");
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else {
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }


        List<Map<String, Object>> resultMap = lowEfficiencyEnterprisesAnalysisService.queryLowYdmmzb(params);
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询指标
     * @param params
     * @return
     */
    @GetMapping("/queryIndex")
    public AjaxResult queryIndex(JcQueryParam params){
        if(StringUtils.isBlank(params.getYears())){
            return AjaxResult.warn("请传入年份");
        }

        String years = params.getYears();
        Integer yearInte = Integer.valueOf(years);
        Integer endTime = yearInte - 5;
        params.setEndYear(String.valueOf(endTime));

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(sysUser.isAdmin()){
            if(StringUtils.isBlank(params.getCity())){
                params.setProvince("370000");
            }else{
                if(StringUtils.endsWith(params.getCity(),"0000")){
                    params.setProvince(params.getCity());
                    params.setCity(null);
                }else if(StringUtils.endsWith(params.getCity(),"00")){
                    params.setCity(params.getCity());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        }else {
            if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCounty(sysUser.getDistrict());
                }else{
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setCity(sysUser.getCity());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
                if(StringUtils.isBlank(params.getCity())){
                    params.setProvince(sysUser.getProvince());
                }else{
                    if(StringUtils.endsWith(params.getCity(),"0000")){
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    }else if(StringUtils.endsWith(params.getCity(),"00")){
                        params.setCity(params.getCity());
                    }else{
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }


        Map<String,Object> result = lowEfficiencyEnterprisesAnalysisService.queryIndex(params);
        return AjaxResult.success(result);
    }
}
